﻿using God.ShoppingWebApi.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Users;
using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using God.ShoppingWebApi.DTO.Order;

namespace God.ShoppingWebApi.WebAdmin
{
    public class AddminOederService : ApplicationService, IAddminOederService
    {
        IRepository<Order, Guid> _repository;
        public AddminOederService(IRepository<Order, Guid> repository)
        {

            _repository = repository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="commodInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task AddAsync(OrderDTO orderDTO)
        {
            var worker = new IdWorker(1, 1);
            orderDTO.OrderNo = worker.NextId().ToString();
            Order order = new Order(GuidGenerator.Create(), orderDTO.OrderNo, orderDTO.OrderStatus,  orderDTO.PayType,  orderDTO.ShippingType, CurrentUser.Id);
            await _repository.InsertAsync(order);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(Guid gu)
        {
            await _repository.DeleteAsync(gu);
        }
        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OrderDTO> Edit(Guid gid)
        {
            var data = await _repository.GetAsync(gid);
            var CommDTO = ObjectMapper.Map<Order, OrderDTO>(data);
            return CommDTO;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<OrderDTO>> GetListAsync()
        {
            var str = (from x in await _repository.GetListAsync()
                       select new OrderDTO
                       {
                           Id = x.Id,
                           PayType = x.PayType,
                           PayTime = x.PayTime,
                           ShippingType = x.ShippingType,
                           OrderStatus = x.OrderStatus,
                           OrderNo = x.OrderNo,
                           CreateTime = x.CreateTime,
                       });
            return str.ToList();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="infoDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OrderDTO> Update(OrderDTO infoDTO)
        {
            var data = await _repository.GetAsync(infoDTO.Id);
            if (data != null)
            {
                data.OrderNo = infoDTO.OrderNo;
                data.CreateTime = infoDTO.CreateTime;
                data.ShippingType = infoDTO.ShippingType;
                data.OrderStatus = infoDTO.OrderStatus;
                data.PayType = infoDTO.PayType;
                data.PayTime = DateTime.Now;
                var Info = await _repository.UpdateAsync(data);

                return infoDTO;
            }
            else
            {
                return null;
            }
        }
    }
}
